package Servlet.camera;

import Dao.UserDao;
import Manage.Camera;
import Utils.mybatisUtils;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class cameraServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter pw=response.getWriter();
        SqlSession session= mybatisUtils.getSqlSession();
        UserDao dao= session.getMapper(UserDao.class);
        ServletContext application=request.getServletContext();
        String username=(String)application.getAttribute("username");
        List<Camera> list=new ArrayList<>();
        list=dao.selectcam(username);
        //表格修饰
        pw.print("    <style type=\"text/css\">\n" +
                "        /*表格样式*/\n" +
                "        table {\n" +
                "            width: auto;\n" +
                "            background: #ccc;\n" +
                "            margin: 10px auto;\n" +
                "            border-collapse: collapse;/*border-collapse:collapse合并内外边距(去除表格单元格默认的2个像素内外边距*/\n" +
                "        }\n" +
                "        th,td {\n" +
                "            height: 25px;\n" +
                "            line-height: 25px;\n" +
                "            text-align: center;\n" +
                "            border: 1px solid #ccc;\n" +
                "        }\n" +
                "        th {\n" +
                "            background: #eee;\n" +
                "            font-weight: normal;\n" +
                "        }\n" +
                "        tr {\n" +
                "            background: #fff;\n" +
                "        }\n" +
                "        tr:hover {\n" +
                "            background: #cc0;\n" +
                "        }\n" +
                "        td a {\n" +
                "            color: #06f;\n" +
                "            text-decoration: none;\n" +
                "        }\n" +
                "        td a:hover {\n" +
                "            color: #06f;\n" +
                "            text-decoration: underline;\n" +
                "        }\n" +
                "        input{\n" +
                "            border-style:none;\n" +
                "        }\n" +
                "        .mybt{\n" +
                "            background-color: white;\n" +
                "            color: blue;\n" +
                "        }\n" +
                "    </style>");
        pw.print("        <table border=\"1px\" align=\"center\">\n" +
                "            <tr>\n" +
                "                <th>摄像头ID</th>\n" +
                "                <th>摄像头所在街道</th>\n" +
                "            </tr>\n" );
        for (Camera camera:list){
            pw.print("       <form action=\"/user/caozuo\">" +
                    "        <tr>\n" +
                    "            <td hidden><input type=\"text\" name=\"username\" value="+username+"></td>\n" +
                    "            <td><input type=\"text\" readonly name=\"keyword\" value="+camera.getKeyword()+"></td>\n" +
                    "            <td><input type=\"text\" name=\"streetmsg\" value="+camera.getStreet()+"></td>\n" +
                    "             <td>\n" +
                    "                    <input class=\"mybt\" type=\"submit\" name=\"caozuo\" onclick=\"return confirm('您正在进行数据删除操作，请确认！！！')\" value=\"删\">\n" +
                    "                    |\n" +
                    "                    <input class=\"mybt\" type=\"submit\" name=\"caozuo\" value=\"改\" >\n" +
                    "                </td>" +
                    "        </tr>\n" +
                    "        </form> ");
        }
        pw.print("       </table>");
    }
}
